{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2cc4d02f-4a0d-4afe-9054-1c282e2485e8",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "71b3352f-d7ec-451f-8ded-b850f3945895",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from models import iStar\n",
    "from datasets import VisiumHDData\n",
    "from profiles import VisiumProfile, VisiumHDProfile"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f17a51b-7aea-4c22-a470-de769e4b140a",
   "metadata": {},
   "source": [
    "## 从 VisiumHD 生成 Visium\n",
    "读取文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4a396427-4b3a-43f0-bcc3-483fcc5b5919",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start load\n",
      "Finish load in 24.6589 s\n"
     ]
    }
   ],
   "source": [
    "path = '/data/datasets/Visium_HD_Human_Tonsil_Fresh_Frozen/binned_outputs/square_002um'\n",
    "# path = '/mnt/TenTA-f702/user/zhangyichi/Data/Visium_HD_Human_Tonsil_Fresh_Frozen/binned_outputs/square_002um'\n",
    "imagefile = '/data/datasets/Visium_HD_Human_Tonsil_Fresh_Frozen/Visium_HD_Human_Tonsil_Fresh_Frozen_tissue_image.tif'\n",
    "# imagefile = '/mnt/TenTA-f702/user/zhangyichi/Data/Visium_HD_Human_Tonsil_Fresh_Frozen/Visium_HD_Human_Tonsil_Fresh_Frozen_tissue_image.tif'\n",
    "rawdata = VisiumHDData()\n",
    "rawdata.load(path=path, source_image_path=imagefile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "003d6a62-b8be-4c6f-a93f-934d94b302fe",
   "metadata": {},
   "source": [
    "设定 visium 配置，slide_serial 可以参考 [10x手册](https://www.10xgenomics.com/support/software/space-ranger/latest/analysis/inputs/image-slide-parameters)\n",
    "- V1: Visium Spatial Gene Expression Slide (v1, 6.5 mm)\n",
    "- V4: Visium CytAssist Spatial Gene Expression Slide (v2, 6.5 mm)\n",
    "- V5: Visium CytAssist Spatial Gene Expression Slide (v2, 11 mm)\n",
    "\n",
    "注意：V1 和 V4 虽然捕获区域大小一致，但是 spot 的默认排序方法不一致。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "105e0ebc-ec9f-4d6c-8b91-2e96c71550e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "visium_profile = VisiumProfile(slide_serial=4)\n",
    "visium_profile.spot_step = 99.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67801feb-ba9f-4488-a63e-d7a65ae2ac11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Merge the gene expression from bins to the spot\n",
      "[██████████████████████████████████████████████████] 100.00%"
     ]
    }
   ],
   "source": [
    "emulate_visium = rawdata.HD2Visium(visium_profile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be0b23bf-0037-4c64-b825-9a54a6ca1df4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start save\n",
      "Finish save in 7.5727 s\n"
     ]
    }
   ],
   "source": [
    "save_path = Path('test/HD2Visium')\n",
    "emulate_visium.save(save_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b028c07d-36f5-444f-8b0f-a17de4a2df8f",
   "metadata": {},
   "source": [
    "## 使用 iStar 生成 Visium 的高分辨率图像 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3c7ce96f-3cb8-4278-a3a6-451138a35c01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start load\n",
      "Finish load in 1.0402 s\n"
     ]
    }
   ],
   "source": [
    "visium_path = Path('test/HD2Visium')\n",
    "iStar_visium = iStar()\n",
    "iStar_visium.load(path=visium_path, profile=visium_profile, source_image_path=visium_path/\"image.tif\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9bdc6048-172c-4ca9-8182-b710bbef20bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = iStar_visium.tissue_mask(auto_mask=True)\n",
    "iStar_visium.select_HVG(n_top_genes=2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dc459fe8-9ee4-425d-8731-88b9e3ab3ade",
   "metadata": {},
   "outputs": [],
   "source": [
    "iStar_visium.set_super_pixel_size(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "348a0969-57e1-4694-9796-e738971c0db5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "istar_dir = Path(\"test/istar\")\n",
    "iStar_visium.save_inpout(istar_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2774c381-e005-45eb-9af5-bbb813c0e25a",
   "metadata": {},
   "source": [
    "conda 环境切换到 iStar， 在 shell 下运行 iStar 工具"
   ]
  },
  {
   "cell_type": "raw",
   "id": "4ccff348-e1d6-4360-b139-99a4dc055a7d",
   "metadata": {},
   "source": [
    "$ ./Run-iStar test/istar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcd1302c-994f-46fb-bade-89177808e774",
   "metadata": {},
   "source": [
    "iStar 工具结束后读取结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "97381753-dcbb-48a6-bfc6-62c4d02c0270",
   "metadata": {},
   "outputs": [],
   "source": [
    "iStar_visium.load_output(istar_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ab3c3b3c-833b-4ed0-acb9-58bea2d55d15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start to_h5ad\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yiriso/anaconda3/envs/DataReader/lib/python3.10/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.\n",
      "  warnings.warn(\"Transforming to str index.\", ImplicitModificationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finish to_h5ad in 0.8186 s\n"
     ]
    }
   ],
   "source": [
    "# iStar_visium.to_csv()\n",
    "iStar_visium.to_h5ad()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ab55cc9-fbc3-4833-b40b-cfd0de84bc79",
   "metadata": {},
   "source": [
    "## 使用 iStar 从 Visium 生成 VisiumHD 数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d1f6860-b84f-4425-be11-f4e7b9581223",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start load\n",
      "Finish load in 6.6569 s\n"
     ]
    }
   ],
   "source": [
    "visium_path = Path('test/HD2Visium')\n",
    "iStar_visium = iStar()\n",
    "iStar_visium.load(path=visium_path, profile=visium_profile, source_image_path=visium_path/\"image.tif\")\n",
    "iStar_visium.tissue_mask(auto_mask=True)\n",
    "iStar_visium.select_HVG(n_top_genes=2000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4e33a5b-5005-4b81-828f-bedcc52ba482",
   "metadata": {},
   "source": [
    "设定 VisiumHD 配置，bin_size 决定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "081fef6a-93b1-47f6-8c8c-2d85c01c09ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "visiumHD_profile = VisiumHDProfile(bin_size=16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4cc2af43-7dda-416c-a2ff-8aef97e503f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "visiumHD_demo = iStar_visium.Visium2HD(HDprofile=visiumHD_profile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "321071ae-723a-4072-bbc1-0937e995ce53",
   "metadata": {},
   "outputs": [],
   "source": [
    "iStar_visium.set_target_VisiumHD(visiumHD_demo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a9b70e9f-e136-452a-936f-8ff492f87c4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "istarHD_dir = Path(\"test/istarHD\")\n",
    "iStar_visium.save_inpout(istarHD_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96eb5db9-8a47-4132-9fad-1dec00c6f51f",
   "metadata": {},
   "source": [
    "conda 环境切换到 iStar， 在 shell 下运行 iStar 工具"
   ]
  },
  {
   "cell_type": "raw",
   "id": "23f1da9a-de72-4dae-af97-c614ddf14166",
   "metadata": {},
   "source": [
    "$ ./Run-iStar test/istar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "499909f1-a009-49ed-9b10-bfe64f46addc",
   "metadata": {},
   "source": [
    "iStar 工具结束后读取结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "619d0bd0-120d-41ba-a16f-e327cfad7142",
   "metadata": {},
   "outputs": [],
   "source": [
    "iStar_visium.load_output(istarHD_dir)\n",
    "iStar_visium.to_VisiumHD(visiumHD_demo, istarHD_dir/\"VisiumHD_result\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:DataReader]",
   "language": "python",
   "name": "conda-env-DataReader-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
